package com.rran.study.designpattern.factory.builder;

import lombok.Data;

/**
 * @author yy
 * @Type Car.java
 * @Desc
 * @date 2020/7/23 13:08
 */
@Data
public class Car {

    //轮子 require
    private String wheel;

    //车身结构 require
    private String skeleton;

    //发动机 require
    private String engine;

    //颜色 optional
    private String color;

    //车膜 optional
    private String film;


    private Car(CarBuilder carBuilder){
        wheel = carBuilder.wheel;
        skeleton = carBuilder.skeleton;
        engine = carBuilder.engine;
        color = carBuilder.color;
        film = carBuilder.film;
    }

    public static class CarBuilder {
        private String wheel;
        private String skeleton;
        private String engine;
        private String color;
        private String film;

        public CarBuilder(String wheel, String skeleton, String engine) {
            this.wheel = wheel;
            this.skeleton = skeleton;
            this.engine = engine;
        }

        public CarBuilder setColor(String color) {
            this.color = color;
            return this;
        }

        public CarBuilder setFilm(String film) {
            this.film = film;
            return this;
        }
        public Car build() {
            // 这里定义依赖关系或者约束条件的校验逻辑
            return new Car(this);
        }
    }

    public static void main(String[] args) {
        //发动机是必选项目，颜色和车膜是非必选
        Car car = new Car.CarBuilder("4只轮子","SUV","二代发动机")
                .setColor("blue")
                .setFilm("M3")
                .build();
        Car car1 =new Car.CarBuilder("3只轮子","Car","五代发动机")
                .setColor("yellow")
                .build();
        System.out.println(car.toString());
        System.out.println(car1.toString());
    }
}
